<template>
	<view>
		<view class="bg-gradual-blue">
			<scroll-view scroll-y class="DrawerPage" :class="modalName=='viewModal'?'show':''">
				
				<view v-bind:style="{width:videoObj.width+'upx',height:videoObj.height+'upx'}">
					<video style="width: 100%;" :src="videoObj.tempFilePath"></video>
				</view>
				<view class="cu-form-group">
					<view class="title text-black">简介:</view>
					<input maxlength="20" placeholder="请输入视频简介" @input="onKeyInput" name="input">{{inputValue}}</input>
				</view>
				<view class='padding margin text-center'>
					<view class='cu-btn bg-green lg block shadow radius margin-xl' @tap="showModal" data-target="viewModal">
						选择BGM
					</view>
					<view class='cu-btn bg-blue lg block shadow radius margin-xl' @tap="videoUpload">
						上传
					</view>
					<view class='cu-btn bg-pink lg block shadow radius margin-xl' @tap="back">
						返回
					</view>
				</view>
				 
			</scroll-view>
			<view class="DrawerClose" :class="modalName=='viewModal'?'show':''" @tap="hideModal">
				<text class="cuIcon-pullright"></text>
			</view>
			<scroll-view scroll-y class="DrawerWindow" :class="modalName=='viewModal'?'show':''">
				<view class="cu-bar search bg-white">
					<view class="search-form round">
						<text class="cuIcon-search"></text>
						<input @focus="InputFocus" @blur="InputBlur"  type="text" placeholder="搜索更多bgm" confirm-type="search"></input>
					</view>
					<view class="action">
						<button class="cu-btn bg-green shadow-blur round">搜索</button>
					</view>
				</view>
				<view class="cu-list menu card-menu margin-top-xl margin-bottom-xl shadow-lg">
					<view class="cu-item arrow" v-for="item in bgm" :key="item.id" >
						<view class="content" :data-item="item" @click="playMusic">
							<text class="iconfont icon-yinyue text-grey" style="margin-right: 30upx;"></text>
							<text class="text-grey" style="margin-right: 30upx;">{{item.name}}</text>
							<text v-if="curBgm == item.id" class="text-blue iconfont icon-bofang "  style="font-size: 25px;line-height: 50upx;"></text>
						</view>
					</view>	
				</view>
				
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	}from 'vuex'
	export default {
		computed:{
			...mapState(['userInfo','webUrl']),
		},
		onLoad(e) {
			this.videoObj = e
			const innerAudioContext = uni.createInnerAudioContext();
			this.audioContext = innerAudioContext
			uni.request({
				url: this.webUrl+"selectBgm",
				success: (res) => {
					this.bgm = res.data.data;
				}
			})
		},
		data() {
			return {
				modalName:null,
				inputValue:'',
				bgm:'',
				curBgm:'',
				isPlay:false,
				audioContext:'',
				videoObj:{}
			};
		},
		methods: {
			onKeyInput: function(event) {
            this.inputValue = event.target.value
			},
			showModal(e) {
				this.modalName = e.currentTarget.dataset.target
			},
			hideModal(e) {
				this.modalName = null
			},
			playMusic: function(e) {
				var item = e.currentTarget.dataset.item
				this.curBgm = item.id
				if(this.audioContext.src == this.webUrl+item.path){
					this.audioContext.pause();
					this.curBgm = ''
				}else {
					this.audioContext.src = this.webUrl+item.path;
					this.audioContext.play()
					this.audioContext.onPlay(() => {
					  console.log('开始播放');
					});
					this.audioContext.onError((res) => {
					  console.log(res.errMsg);
					  console.log(res.errCode);
					});		
				}
						
			},
			back(){
				uni.navigateBack({})
			},
			videoUpload(){
				if(this.inputValue == ''){
					uni.showToast({
						icon:"none",
						title:"简介不能为空",
						duration:2000
					})
					return;
				}
				this.audioContext.pause();
				var userid = this.userInfo.id
				var bgmid = this.curBgm
				var file = this.videoObj.tempFilePath
				var seconds = this.videoObj.duration
				var height = this.videoObj.height
				var width = this.videoObj.width
				var desc = this.inputValue
				uni.showLoading({
					title:"上传中"
				})
				uni.uploadFile({
					url:this.webUrl+"videoUpload",
					filePath: file,
					name: 'file',
					formData: {
						'userId':userid,
						'bgmId' :bgmid,
						'seconds':seconds,
						'width':width,
						'height':height,
						'desc':desc
					},
					success:(uploadFileRes) => {
						if(uploadFileRes.statusCode==200){
							console.log(uploadFileRes.data)
							console.log(uploadFileRes.statusCode)
							var obj = JSON.parse(uploadFileRes.data);
							console.log(obj)
							uni.showToast({
								title:"上传成功",
								duration:3000
							})
							var now = "login"
							this.$store.dispatch('NavChanges',now)
							uni.navigateTo({
								url:"../home/home"
							})
						}
					  	if(uploadFileRes.statusCode==404){
							console.log(uploadFileRes.data)
							console.log(uploadFileRes.statusCode)
							uni.hideLoading();
							that.modalName=null
							uni.showToast({
								title:"上传失败",
								icon:"none"
							})
						}
						return;
					}
				})
			}
		},
	}
</script>

<style>
	page {
		background-image: var(--gradualBlue);
		width: 100vw;
		overflow: hidden;
	}

	.DrawerPage {
		position: fixed;
		width: 100vw;
		height: 100vh;
		left: 0vw;
		background-color: #f1f1f1;
		transition: all 0.4s;
	}

	.DrawerPage.show {
		transform: scale(0.9, 0.9);
		left: 85vw;
		box-shadow: 0 0 60upx rgba(0, 0, 0, 0.2);
		transform-origin: 0;
	}

	.DrawerWindow {
		position: absolute;
		width: 85vw;
		height: 100vh;
		left: 0;
		top: 0;
		transform: scale(0.9, 0.9) translateX(-100%);
		opacity: 0;
		pointer-events: none;
		transition: all 0.4s;
		padding: 100upx 0;
	}

	.DrawerWindow.show {
		transform: scale(1, 1) translateX(0%);
		opacity: 1;
		pointer-events: all;
	}

	.DrawerClose {
		position: absolute;
		width: 40vw;
		height: 100vh;
		right: 0;
		top: 0;
		color: transparent;
		padding-bottom: 30upx;
		display: flex;
		align-items: flex-end;
		justify-content: center;
		background-image: linear-gradient(90deg, rgba(0, 0, 0, 0.01), rgba(0, 0, 0, 0.6));
		letter-spacing: 5px;
		font-size: 50upx;
		opacity: 0;
		pointer-events: none;
		transition: all 0.4s;
	}

	.DrawerClose.show {
		opacity: 1;
		pointer-events: all;
		width: 15vw;
		color: #fff;
	}

	.DrawerPage .cu-bar.tabbar .action button.cuIcon {
		width: 64upx;
		height: 64upx;
		line-height: 64upx;
		margin: 0;
		display: inline-block;
	}

	.DrawerPage .cu-bar.tabbar .action .cu-avatar {
		margin: 0;
	}

	.DrawerPage .nav {
		flex: 1;
	}

	.DrawerPage .nav .cu-item.cur {
		border-bottom: 0;
		position: relative;
	}

	.DrawerPage .nav .cu-item.cur::after {
		content: "";
		width: 10upx;
		height: 10upx;
		background-color: currentColor;
		position: absolute;
		bottom: 10upx;
		border-radius: 10upx;
		left: 0;
		right: 0;
		margin: auto;
	}

	.DrawerPage .cu-bar.tabbar .action {
		flex: initial;
	}
</style>
